package com.zhentao.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;


import com.zhentao.pojo.Middle;
import com.zhentao.pojo.Users;
import com.zhentao.service.MerchantService;
import com.zhentao.service.MiddleService;
import com.zhentao.service.RoleService;
import com.zhentao.service.UsersService;


import com.zhentao.util.ResponseResult;
import com.zhentao.util.TextModerationPlusUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/role")
public class ApUserRole {
    @Autowired
    RoleService roleService;
    @Autowired
    UsersService usersService;
    @Autowired
    MiddleService middleService;
    @Autowired
    MerchantService merchantService;
    @RequestMapping("findAdd")
    public String findAdd(@RequestBody Users users) {
        Middle middle = new Middle();
        ResponseResult responseResult= TextModerationPlusUtil.checkContent(users.getUsername());
        if(responseResult.getCode()==600){
            usersService.save(users);
            middle.setUserId(users.getUserId());
            middle.setRoleId(users.getRoles());
            middleService.save(middle);
            return "普通添加成功";
        }else {
            return "审核失败";
        }



    }
    @RequestMapping("findAll")
    public List<Users> findAll(){
        List<Users> list = usersService.list();
        return list;
    }

    @RequestMapping("delete")
    public String delete(Integer userId){
        usersService.removeById(userId);
        QueryWrapper<Middle> qw=new QueryWrapper<>();
        qw.eq("user_id",userId);
        middleService.remove(qw);
        return "删除成功";
    }
    @RequestMapping("update")
    public String update(@RequestBody Users users){
        Users users1=new Users();
        users1.setUsername(users.getUsername());
        users1.setPassword(users.getPassword());
        users1.setPhone(users.getPhone());
        usersService.updateById(users);
        return "修改成功";
    }
}